package com.zb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import com.zb.emq.EmqClient;
import com.zb.entity.TbQuotaModel;
import com.zb.service.TbQuotaService;
import com.zb.util.R;
import com.zb.vo.QuotaPageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName TbQuotaController
 * @Description 控制器
 * @Author xm
 * @Date 2024/08/07 11:42
 **/
@RestController
@RequestMapping("/tb-quota-model")
@Api(value = "TbQuotaController", tags = {"指标控制器"})
public class TbQuotaController {

    @Autowired
    public TbQuotaService tbQuotaModelService;
    @Autowired
    private EmqClient emqClient;

    @GetMapping("/number-quota")
    public List<TbQuotaModel> queryNumberQuota() {
        return tbQuotaModelService.queryNumberQuota();
    }

    @PostMapping("/page")
    public R page(@RequestBody QuotaPageVO quotaPageVO) {
        LambdaQueryWrapper<TbQuotaModel> qw = new LambdaQueryWrapper<>();
        if (!Strings.isNullOrEmpty(quotaPageVO.getName())) {
            qw.like(TbQuotaModel::getName, quotaPageVO.getName());
        }
        Page<TbQuotaModel> page = new Page<>(quotaPageVO.getIndex(), quotaPageVO.getSize());
        tbQuotaModelService.page(page, qw);
        System.out.println(page);
        return new R(200, page);
    }






    @PostMapping("/save")
    public R saveQuota(@RequestBody TbQuotaModel tbQuotaModel) {
        if (tbQuotaModelService.save(tbQuotaModel)) {
            try {
                emqClient.subscribe(tbQuotaModel.getSubject());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new R(200, "添加指标成功");
        } else {
            return new R(200, "添加指标失败");
        }
    }

    @PostMapping("/update")
    public R updateQuota(@RequestBody TbQuotaModel tbQuotaModel) {
        if (tbQuotaModelService.updateById(tbQuotaModel)) {
            return new R(200, "修改指标成功");
        } else {
            return new R(200, "修改指标失败");
        }
    }

    @GetMapping("/remove/{id}")
    public R removeQuota(@PathVariable("id") Integer id) {
        if (tbQuotaModelService.removeById(id)) {
            return new R(200, "删除指标成功");
        } else {
            return new R(200, "删除指标失败");
        }
    }

    @GetMapping("/info/{id}")
    public TbQuotaModel queryNumberQuota(@PathVariable("id") Integer id) {
        return tbQuotaModelService.getById(id);
    }


}
